<?php

namespace App\Http\Controllers\Sys_api;


use App\Http\Controllers\Controller;
use App\Models\EquipmentModel;
use App\Models\EquipmentPortModel;
use Illuminate\Http\Request;
use App\Models\StationModel;
use App\Models\StationFAVModel;
use App\Models\StationImgModel;
use Nette\Utils\Json;

/**
 * 充电站
 */
class StationAPIController extends BaseAPIController
{
    /**
     * 充电站列表
     *
     * @return void
     */
    public function StationListAPI(Request $request)
    {
        $page = $request->page;
        $map[] = ['station_id', '!=', '""'];
        $sort = ['id', 'desc'];
        $distance = 10;
        $id = $request->id;
        $latitude = $request->latitude;
        $longitude = $request->longitude;
        $data = [];
        $keyWord = $request->keyWord;
        $latArr = $this->GetMaxOrMinLat($distance, $latitude);
        $lngArr = $this->GetMaxOrMinLng($distance, $latitude, $longitude);
        //
        $list = StationModel::GetLatLngStationList($this->pageSize, $page, $map, $sort, $latArr, $lngArr, $keyWord, $latitude, $id);
        if (!$list) {
            $arr = [
                'total' => 0,
                'data' => []
            ];
            return $this->rjson($arr);
        }
        foreach ($list['data'] as $key => $value) {
            //查找充电桩数量
            $eqNum = EquipmentModel::GetEqNum($value['id']);
            if ($eqNum > 0) {
                $eqList = EquipmentModel::where('station_id', $value['id'])->get();
                //查找充电端口数量
                if ($eqList) {
                    foreach ($eqList  as $k => $v) {
                        $portNum = EquipmentPortModel::where('eq_id', $v['id'])->count();
                        $eq_port_kx_num = EquipmentPortModel::where('eq_id', $v['id'])->where('port_status', 0)->count();
                        $portData = [
                            'kx_num' => $eq_port_kx_num,
                            'num' => intval($portNum) * intval($eqNum),
                            'eq_id' => $v['id']
                        ];
                    }
                }
            } else {
                $portData = [];
            }
            //计算坐标之间的距离
            $dist = $this->getDistanceBy2Point([$longitude, $latitude], [$value['longitude'], $value['latitude']]);
            $data[] = [
                'id' => $value['id'],
                'station_name' => $value['station_name'],
                'station_adds' => $value['station_adds'],
                'latitude' => $value['latitude'],
                'longitude' => $value['longitude'],
                'dist' => $dist,
                'eqNum' => $eqNum,
                'portData' => $portData,
                'status' => $value['station_status'] == 0 ? 0 : 1,
                'height' =>40,
                'width' =>40,
            ];
        };

        //排序
        $data = $this->arraySort($data, 'dist');

        $arr = [
            'total' => $list['total'],
            'data' => $data
        ];
        return $this->rjson($arr);
    }



    /**
     * 充电站详细 by id
     *
     * @return void
     */
    public function StationInfoAPI(Request $request)
    {
        $id = $request->input('id');
        $total = 0;
        $map[] = ['id', "=", $id];
        $images = [];
        $uid = $request->input('uid');
        $res = StationModel::GetStationInfo($map);
        $issc = 0;
        if ($res) $total = 1;
        //获取充电端口信息
        $portList = EquipmentPortModel::GetEqPortList($id);
        //获取充电站照片信息
        $imgs = StationImgModel::where('station_id', $id)->get();
        foreach ($imgs as $k => $v) {
            $images[] = $v['img_path'];
        }
        //是否是用户收藏
        $fvStation = StationFAVModel::where('station_id',$id)->where('user_id',$uid)->first();
        if($fvStation){
            $issc = 1;
        }
        $arr = [
            'id' => $res['id'],
            'station_name' => $res['station_name'],
            'station_adds' => $res['station_adds'],
            'portList' => $portList,
            'images' => $images,
            'issc' => $issc
        ];
        $info = [
            'data' => $arr,
            'total' => $total ?: 0
        ];
        return $this->rjson($info);;
    }


    /**
     * 添加站点收藏
     *
     * @return void
     */
    public function AddStationFavoriteAPI(Request $request)
    {
        $data['station_id'] = $request->input('id');
        $data['user_id'] = $request->input('uid');
        $res = StationFAVModel::AddFavStation($data);
        return  $res;
    }



    /**
     * 站点收藏列表
     *
     * @return void
     */
    public function StationFavoriteListAPI(Request $request)
    {
        $page = $request->page;
        $data = [];
        $map[] = ['station_fav.user_id', '=', $request->input('uid')];
        $sort = ['station_fav.id', 'desc'];
        $list = StationFAVModel::GetFavStationList($this->pageSize, $page, $map, $sort);
        foreach ($list['data'] as $key => $value) {
            //查找充电桩数量
            $eqNum = EquipmentModel::GetEqNum($value['id']);
            if ($eqNum > 0) {
                $eqList = EquipmentModel::where('station_id', $value['id'])->get();
                //查找充电端口数量
                if ($eqList) {
                    foreach ($eqList  as $k => $v) {
                        $portNum = EquipmentPortModel::where('eq_id', $v['id'])->count();
                        $eq_port_kx_num = EquipmentPortModel::where('eq_id', $v['id'])->where('port_status', 0)->count();
                        $portData = [
                            'kx_num' => $eq_port_kx_num,
                            'num' => intval($portNum) * intval($eqNum),
                            'eq_id' => $v['id']
                        ];
                    }
                }
            } else {
                $portData = [];
            }
           
            $data[] = [
                'id' => $value['id'],
                'station_name' => $value['station_name'],
                'station_adds' => $value['station_adds'],
                'latitude' => $value['latitude'],
                'longitude' => $value['longitude'],
                'eqNum' => $eqNum,
                'portData' => $portData,
                'status' => $value['station_status'] == 0 ? 0 : 1,
                'height' =>40,
                'width' =>40,
            ];
        };
        $arr = [
            'total' => $list['total'],
            'data' => $data
        ];
        return $this->rjson($arr);
        return $this->rjson($list);
    }


    /**
     * 站点收藏删除
     *
     * @return void
     */
    public function StationFavoriteDelAPI(Request $request)
    {
        $id = $request->input('id');
        $map = [
            ['column' => 'user_id', 'operator' => '=', 'value' => $request->input('uid')],
            ['column' => 'station_id', 'operator' => '=', 'value' => $id]
        ];
        $res = StationFAVModel::DelFavStationByMap($map);
        return $res;
    }
}
